<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<link rel="shortcut icon" href="../static/favicon.ico" th:href="@{favicon.ico}"/>
	<th:block th:include="include :: header('修改商品分类')" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-TbCategory-edit" th:object="${goods}">
			<input id="goodsId" type="hidden" th:field="*{goodsId}" />
			<div class="form-group">
				<label class="col-sm-2 control-label is-required">商品名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" th:field="*{goodsName}" id="goodsName" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label">商品供价：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" id="costPrice" th:field="*{costPrice}">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label">商品售价：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" id="sellPrice" th:field="*{sellPrice}">
				</div>
			</div>
			<div class="form-group" id="div_brother">
				<label class="col-sm-2 control-label">商品库存：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" id="inventory" th:field="*{inventory}">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label">商品图片：</label>
				<div class="col-sm-8">
					<input type="file" name="file" class="form-control" onchange="setImage(this);" id="img">
					<img th:src="${imgUrl}" style="width: 400px" alt="User Image" id="image">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label">商品状态：</label>
				<div class="col-sm-8">
					<select id="status" class="form-control">
						<option value="0" selected="selected">上架</option>
						<option value="1">下架</option>
						<option value="2">已删除</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label">修改人：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" id="updateId" th:field="*{updateId}">
				</div>
			</div>
		</form>
		<div class="col-sm-offset-5 col-sm-10">
			<button style="margin-right: 10px" type="button" class="btn btn-sm btn-primary" onclick="editGoods()"><i class="fa fa-check"></i>保 存
			</button>&nbsp;
			<button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭
			</button>
		</div>	</div>
	<th:block th:include="include :: footer" />
	<script  th:inline="javascript">
		var specArray=[[${specArray}]];
		var url=[[${imgUrl}]];

		for(var i=0;i<specArray.length;i++){
			for(var key in specArray[i])
			var temp='<div class="form-group"><label class="col-sm-2 control-label">'+key+'</label>\n' +
					'\t\t\t\t<div class="col-sm-8">\n' +
					'\t\t\t\t\t<input class="form-control" type="text" name="inventory" value="'+specArray[i][key]+'"  readonly="true">\n' +
					'\t\t\t\t</div></div>';
			$("#div_brother").after(temp);
		}
		var prefix = ctx + "TbGoods";
		

		
		function editGoods() {
	        if ($.validate.form()) {
	        	var goods={};
				goods.goodsName=$('#goodsName').val();
				goods.goodsId=$('#goodsId').val();
				goods.costPrice=$('#costPrice').val();
				goods.sellPrice=$('#sellPrice').val();
				goods.inventory=$('#inventory').val();
				goods.updateId=$('#updateId').val();
				goods.status=$('#status option:selected').val();
				//获取图片
				var formData = new FormData();
				var docObj = document.getElementById("img");

				if(docObj!=null){
					formData.append('file',docObj.files[0]);
					//上传图片
					$.ajax({
						url: ctx + "imgUpload/testUpload",
						type: 'post',
						cache: false,
						data: formData,
						async: false,
						processData: false,
						contentType: false,
						dataType: "json",
						success: function (result) {
							if (result.code == web_status.SUCCESS) {
								goods.bak1=result.msg;
							}else{
								$.modal.alertError(result.msg);
							}
						}
					});
				}
				var config={
					url: prefix + "/editGoods",//请求地址
					method: 'post',//请求方式
					contentType: "application/json",
					data: JSON.stringify(goods),//提交的请求参数，一般为json或者?传参字符串
					dataType: 'json',//是否将响应数据转换为json对象,不添加时，默认是响应数据是字符串类型
					success: function (result) {//响应成功的处理函数，data：返回的响应数据
						if (result.code == web_status.SUCCESS) {
							parent.$.modal.msgSuccess(result.msg);
							closeItem();
							parent.$.table.refresh();
						}else{
							$.modal.alertError(result.msg);
						}
					}
				}
				$.ajax(config);
	        }
	    }

		function setImage(element) {
			var docObj = document.getElementById("img");
			var file = docObj.files[0];
			var imgObjPreview = document.getElementById("image");
			imgObjPreview.src = window.URL.createObjectURL(file);
			return true;
			//先把图片上传至七牛云上，返回一个图片的路径，提交表单时将路径传给后台存入数据库
		}
	</script>
</body>
</html>
